use std::env;

use crate::util;

fn env_or(env: &str, or: &str) -> String {
    env::var(env).unwrap_or(or.to_owned())
}

pub struct Config {
    pub bind_addr: String,
    pub session_key: [u8; 64],
    pub mysql_url: String
}

impl Config {
    pub fn from_env() -> Self {
        Self {
            bind_addr: env_or("APP_BIND_ADDR", "127.0.0.1:8080"),
            session_key: util::sha512(&env_or("APP_SESSION_KEY", "actix-example:secret")),
            mysql_url: env_or("APP_MYSQL_URL", "mysql://root:123456@localhost:3306/actix-example")
        }
    }
}
